package com.titleevaluation.service;

import com.titleevaluation.entity.Application;
import com.titleevaluation.entity.UnitApprovalProof;
import com.titleevaluation.repository.ApplicationRepository;
import com.titleevaluation.repository.UnitApprovalProofRepository;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

@Service
public class UnitApprovalProofService {

    private final UnitApprovalProofRepository unitApprovalProofRepository;
    private final ApplicationRepository applicationRepository;

    public UnitApprovalProofService(UnitApprovalProofRepository unitApprovalProofRepository, ApplicationRepository applicationRepository) {
        this.unitApprovalProofRepository = unitApprovalProofRepository;
        this.applicationRepository = applicationRepository;
    }

    public UnitApprovalProof createUnitApprovalProof(Long applicationId, UnitApprovalProof unitApprovalProof) {
        Application application = applicationRepository.findById(applicationId)
                .orElseThrow(() -> new RuntimeException("Application not found"));
        unitApprovalProof.setApplication(application);
        unitApprovalProof.setCreatedAt(LocalDateTime.now());
        unitApprovalProof.setUpdatedAt(LocalDateTime.now());
        return unitApprovalProofRepository.save(unitApprovalProof);
    }

    public List<UnitApprovalProof> getUnitApprovalProofByApplicationId(Long applicationId) {
        return unitApprovalProofRepository.findByApplicationId(applicationId);
    }

    public Optional<UnitApprovalProof> getUnitApprovalProofById(Long id) {
        return unitApprovalProofRepository.findById(id);
    }

    public UnitApprovalProof updateUnitApprovalProof(Long id, UnitApprovalProof updatedUnitApprovalProof) {
        UnitApprovalProof existingUnitApprovalProof = unitApprovalProofRepository.findById(id)
                .orElseThrow(() -> new RuntimeException("UnitApprovalProof not found"));

        existingUnitApprovalProof.setWorkUnit(updatedUnitApprovalProof.getWorkUnit());
        existingUnitApprovalProof.setUnitApprovalType(updatedUnitApprovalProof.getUnitApprovalType());
        existingUnitApprovalProof.setApproverName(updatedUnitApprovalProof.getApproverName());
        existingUnitApprovalProof.setApproverContact(updatedUnitApprovalProof.getApproverContact());
        existingUnitApprovalProof.setApprovalCertificateType(updatedUnitApprovalProof.getApprovalCertificateType());
        existingUnitApprovalProof.setApprovalCertificateNo(updatedUnitApprovalProof.getApprovalCertificateNo());
        existingUnitApprovalProof.setMaterialUploadTime(updatedUnitApprovalProof.getMaterialUploadTime());
        existingUnitApprovalProof.setOperation(updatedUnitApprovalProof.getOperation());
        existingUnitApprovalProof.setUpdatedAt(LocalDateTime.now());

        return unitApprovalProofRepository.save(existingUnitApprovalProof);
    }

    public void deleteUnitApprovalProof(Long id) {
        unitApprovalProofRepository.deleteById(id);
    }
}

